import { getRepeatOrder, getRepeatOrderSet } from '@/api/order'
import { ref } from 'vue'
import { markRaw } from 'vue'
import { WarningFilled, CircleCloseFilled } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

// /**
//  * 重单字段查询
//  */
// const repeatSearch = ref({
//   "codeIds": [],//设备序列号
//   "goodsIds": [],//设备IDs
//   "moduleId": '',//模块ID
//   "userPhone": "",//公司手机号
//   "username": ""//公司姓名
// })
// //型号、设备名称=>1 序列号、序列号=>2  客户名称=>3 公司手机号=>4 工单模块=>5
const is_RepeatOrder = ref([])
const is_RepeatOrderArr = ref([]) //动态添加
/**
 * 重单开启字段
 */
const repeatOrderObj = ref({
  enableGoodsModel: true, //型号
  enableGoodsName: false, //设备名称
  enableOrderModule: false, //工单模块
  enableSNCode: false, //序列号、序列号
  enableUserName: true, //客户名称
  enableUserPhone: true, //公司手机号
})
// 获取是否开启重单
const getRepeatOrderSetFun = async () => {
  await getRepeatOrderSet().then((res) => {
    if (res.data.code == 20000) {
      repeatOrderObj.value = res.data.data.row
      is_RepeatOrder.value = []
      if (repeatOrderObj.value.enableGoodsModel || repeatOrderObj.value.enableGoodsName) {
        is_RepeatOrder.value.push(1)
      } else {
        is_RepeatOrderArr.value = is_RepeatOrderArr.value.filter((o) => !(o == 1))
      }
      if (repeatOrderObj.value.enableSNCode) {
        is_RepeatOrder.value.push(2)
      } else {
        is_RepeatOrderArr.value = is_RepeatOrderArr.value.filter((o) => !(o == 2))
      }
      if (repeatOrderObj.value.enableUserName) {
        is_RepeatOrder.value.push(3)
      } else {
        is_RepeatOrderArr.value = is_RepeatOrderArr.value.filter((o) => !(o == 3))
      }
      if (repeatOrderObj.value.enableUserPhone) {
        is_RepeatOrder.value.push(4)
      } else {
        is_RepeatOrderArr.value = is_RepeatOrderArr.value.filter((o) => !(o == 4))
      }
      if (repeatOrderObj.value.enableOrderModule) {
        is_RepeatOrder.value.push(5)
      } else {
        is_RepeatOrderArr.value = is_RepeatOrderArr.value.filter((o) => !(o == 5))
      }
    }
  })
}
// 查询是否重单
const getRepeatOrderFun = (dataVo: repeatOrder, val = false) => {
  return getRepeatOrder(dataVo).then((res) => {
    if (res.data.code == 20000) {
      // 判断是否重复报单
      if (res.data.data.row.repeatOrder) {
        // 判断是否开启重复报单
        if (res.data.data.row.enableRepeatOrder) {
          if (res.data.data.row.enableRepeatOrderTip) {
            if (val) {
              ElMessageBox.confirm('存在重复工单，是否继续报单？', '提示', {
                type: 'warning',
                icon: markRaw(WarningFilled),
                confirmButtonText: '继续',
                cancelButtonText: '取消',
              })
                .then(() => {
                  return { code: false, val: true }
                })
                .catch(() => {
                  return { code: true, val: true }
                })
            }
            return { code: true, val: true }
          }
        } else {
          // 不开启重复报单
          if (val) {
            ElMessageBox.confirm('存在重复工单，不允许报单!', '提示', {
              type: 'error',
              icon: markRaw(CircleCloseFilled),
              showCancelButton: false,
            })
          }
          return { code: true, val: false }
        }
      }
    } else {
      ElMessageBox.confirm(res.data.message, '提示', {
        type: 'error',
        icon: markRaw(CircleCloseFilled),
        showCancelButton: false,
      })
    }
    return { code: false, val: false }
  })
}
export default {
  getRepeatOrderSetFun,
  repeatOrderObj,
  getRepeatOrderFun,
  is_RepeatOrder,
  is_RepeatOrderArr,
}
